<?php

class Domain_ServiceBaiduApi {
    private $api_key;
    const api_url = "http://apis.baidu.com";

    public function __construct() {
        $this->api_key = DI()->config->get('app.BaiduApiKey');
    }

    private static function joinParams($path, $params) {
        $url = self::api_url . $path;
        if (substr($path, 0, 5) == "http:" || substr($path, 0, 6) == "https:") {
            $url = $path;
        }
        if (count($params) > 0) {
            $url = $url . "?";
            foreach ($params as $key => $value) {
                $url = $url . $key . "=" . $value . "&";
            }
            $length = strlen($url);
            if ($url[$length - 1] == '&') {
                $url = substr($url, 0, $length - 1);
            }
        }
        return $url;
    }

    static function check($data, $code, $successFlag, $msg) {
        if (isset($data[$code])&&$data[$code] != $successFlag) {
            DI()->logger->log("debug", T("baidu api response error"), json_encode($data));
            throw new PhalApi_Exception(is_array($data[$msg]) ? $data[$msg][0] : $data[$msg], $data[$code]);
        }
    }

     function get($service, $data, $code, $successFlag, $msg, $succes) {
        $curl = new PhalApi_CUrl();
        $url = self::joinParams($service, $data);
        $response = $curl->get($url, 3000, ['apikey:' . $this->api_key,]);
//         var_dump($response);
        $response = @json_decode($response, true);
        self::check($response, $code, $successFlag, $msg);
        return $succes ? $response[$succes] : $response;
    }

    public function shortUrl($url) {
        $code = "errNum";
        $successFlag = 0;
        $msg = "errMsg";
        $succes = "urls";

        return $this->get("/3023/shorturl/shorten", [
            "url_long" => $url,
        ], $code, $successFlag, $msg, $succes);
    }
    public function idService($id = '420984198704207896') {
        $code = "errNum";
        $successFlag = 0;
        $msg = "retMsg";
        $succes = "retData";
        return $this->get("/apistore/idservice/id", ["id" => $id], $code, $successFlag, $msg, $succes);
    }

    public function IPService($ip = '127.0.0.1') {
        $code = "errNum";
        $successFlag = 0;
        $msg = "retData";
        $succes = "retData";

        return $this->get("/apistore/iplookupservice/iplookup", ["ip" => $ip], $code, $successFlag, $msg, $succes);
    }

    public function aqiService($city = 'hangzhou') {
        $code = "errNum";
        $successFlag = 0;
        $msg = "retData";
        $succes = "retData";

        return $this->get("/apistore/aqiservice/aqi", ["city" => $city], $code, $successFlag, $msg, $succes);
    }
 public function weather($city = 'hangzhou') {
        $code = "errNum";
        $successFlag = 0;
        $msg = "retData";
        $succes = "HeWeather data service 3.0";

     $data= $this->get("/heweather/weather/free", ["city" => $city], $code, $successFlag, $msg, $succes);
     if (isset($data[0]['status'])&&$data[0]['status']!="ok") {
         DI()->logger->log("debug", T("baidu api response error"), $data[0]['status']);
         throw new PhalApi_Exception($data[0]['status'], 400);
     }
     unset($data['status']);
     return $data;
    }

    public function phoneService($phone = '13800138000') {
        $code = "error";
        $successFlag = 0;
        $msg = "msg";
        $succes = "data";

        return $this->get("/chazhao/mobilesearch/phonesearch", ["phone" => $phone], $code, $successFlag, $msg, $succes);
    }

    public function historyToday() {
        $code = "error_code";
        $successFlag = 0;
        $msg = "reason";
        $succes = "result";

        return $this->get("/avatardata/historytoday/lookup", [
            "yue"    => date('n', time()),
            "ri"     => date('j', time()),
            "type"   => 1,
            "page"   => 1,
            "rows"   => 50,
            "dtype"  => "JSON",
            "format" => "true",
        ], $code, $successFlag, $msg, $succes);
    }

    public function historyTomorrow() {
        $code = "error_code";
        $successFlag = 0;
        $msg = "reason";
        $succes = "result";

        return $this->get("/avatardata/historytoday/lookup", [
            "yue"    => date('n', strtotime("+1 day")),
            "ri"     => date('j', strtotime("+1 day")),
            "type"   => 1,
            "page"   => 1,
            "rows"   => 50,
            "dtype"  => "JSON",
            "format" => "true",
        ], $code, $successFlag, $msg, $succes);
    }

    public function historyDay($month, $day, $rows) {
        $code = "error_code";
        $successFlag = 0;
        $msg = "reason";
        $succes = "result";

        return $this->get("/avatardata/historytoday/lookup", [
            "yue"    => $month,
            "ri"     => $day,
            "type"   => 1,
            "page"   => 1,
            "rows"   => $rows,
            "dtype"  => "JSON",
            "format" => "true",
        ], $code, $successFlag, $msg, $succes);
    }

    //返回说明http://www.tuling123.com/html/doc/api.html

    public function robot($info) {
        $code = "code";
        $successFlag = true;
        $msg = false;
        $succes = false;

//        return $this->get("turing/turing/turing", [
        return $this->get("http://www.tuling123.com/openapi/api", [
            "key"    => "8a9b08cd9e36c4981d360a01a5013b25",
            "userid" => 11,
            "info"   => $info
        ], $code, $successFlag, $msg, $succes);
    }


}
